package com.example.springbatchdemo.config;

import com.example.springbatchdemo.config.decider.MyDecider;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Classname DeciderDemo
 * @Description 决策器的使用范例
 * @Date 2020/9/19 12:36
 * @Created by kevin
 */
@Configuration
@EnableBatchProcessing
public class DeciderDemo {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    private MyDecider myDecider;

    @Bean
    public Step deciderDemoStep1() {

        return stepBuilderFactory.get("deciderDemoStep1").tasklet(( StepContribution contribution, ChunkContext chunkContext ) -> {
            System.out.println("执行了deciderDemoStep1。。。");
            return RepeatStatus.FINISHED;
        }).build();
    }

    @Bean
    public Step deciderDemoStep2() {

        return stepBuilderFactory.get("deciderDemoStep2").tasklet(( StepContribution contribution, ChunkContext chunkContext ) -> {
            System.out.println("执行了deciderDemoStep2。。。");
            return RepeatStatus.FINISHED;
        }).build();
    }


    @Bean
    public Step deciderDemoStep3() {

        return stepBuilderFactory.get("deciderDemoStep3").tasklet(( StepContribution contribution, ChunkContext chunkContext ) -> {
            System.out.println("执行了deciderDemoStep3。。。");
            return RepeatStatus.FINISHED;
        }).build();
    }

    @Bean
    public Job deciderDemoJob() {
        return jobBuilderFactory.get("deciderDemoJob")
                .start(deciderDemoStep1())
                .next(myDecider)
                .from(myDecider).on("even").to(deciderDemoStep2())
                .from(myDecider).on("odd").to(deciderDemoStep3())
                .from(deciderDemoStep3()).on("*").to(myDecider)
                .end()
                .build();
    }


}
